package org.ruiyichen.guizhou.agr.ui.mineview;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.util.Xml;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;


import org.ruiyichen.guizhou.agr.R;
import org.ruiyichen.guizhou.agr.utils.L;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;

public class SelectPopupWindow extends PopupWindow {


    //    private TextView wxPayBtn, yibaoBtn, cancelBtn;
    private View mMenuView;

    private String[] menuStr;

    private TextView[] menuTv;

    private Activity mContext;

    public SelectPopupWindow(Activity context, OnClickListener itemsOnClick, String[] menuStr) {
        super(context);
        this.mContext = context;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.select_pop_window_layout, null);
        LinearLayout ll = (LinearLayout) mMenuView.findViewById(R.id.pop_layout);

        this.menuStr = menuStr;
        //
        XmlPullParser parser = context.getResources().getLayout(R.layout.textview_select_pop_menu);
        AttributeSet attributes = Xml.asAttributeSet(parser);

        try {
            int type;
            while ((type = parser.next()) != XmlPullParser.START_TAG &&
                    type != XmlPullParser.END_DOCUMENT) {
                // Empty
            }
            if (type != XmlPullParser.START_TAG) {
                L.e("", "the xml file is error!\n");
            }
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //初始化菜单内容
        menuTv = new TextView[menuStr.length];

        LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(context, attributes);
        for (int i = 0; i < menuStr.length; i++) {
            TextView tv = new TextView(context, attributes);
            tv.setId(i);
            tv.setText(menuStr[i]);
//            ll.addView(tv, 0, p);
            tv.setOnClickListener(itemsOnClick);
            ll.addView(tv, i, p);
            menuTv[i] = tv;
        }
//        LinearLayout.LayoutParams params=new LayoutParams(context,attributes);

//        wxPayBtn = (TextView) mMenuView.findViewById(R.id.btn_wxpay);
//        yibaoBtn = (TextView) mMenuView.findViewById(R.id.btn_yibaopay);
        TextView cancelBtn = (TextView) mMenuView.findViewById(R.id.btn_cancel);
        //取消按钮
        cancelBtn.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                //销毁弹出框
                dismiss();
            }
        });


        //设置按钮监听
//        wxPayBtn.setOnClickListener(itemsOnClick);
//        yibaoBtn.setOnClickListener(itemsOnClick);
        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.FILL_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.AnimBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x00000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = mMenuView.findViewById(R.id.pop_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });

    }

    public TextView[] getMenuTv() {
        return menuTv;
    }

//    /**
//     * 设置支付
//     *
//     * @param type 1:易宝支付;2:微信支付;
//     */
//    public void setSelectedType(int type) {
//        switch (type) {
//            case 1:
//                yibaoBtn.setSelected(true);
//                wxPayBtn.setSelected(!yibaoBtn.isSelected());
//                break;
//            case 2:
//                wxPayBtn.setSelected(true);
//                yibaoBtn.setSelected(!wxPayBtn.isSelected());
//                break;
//        }
//    }

    public void selectMenu(int id) {
        if (menuTv == null || menuTv.length == 0) {
            return;
        }
        for (int i = 0; i < menuTv.length; i++) {
            menuTv[i].setSelected(menuTv[i].getId() == id);
        }
    }

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        super.showAtLocation(parent, gravity, x, y);
        WindowManager.LayoutParams params = mContext.getWindow().getAttributes();
        params.alpha = 0.42f;
        mContext.getWindow().setAttributes(params);
    }

    @Override
    public void dismiss() {
        if (isShowing()) {
//            this.dismiss();
            WindowManager.LayoutParams params = mContext.getWindow().getAttributes();
            params.alpha = 1f;
            mContext.getWindow().setAttributes(params);
        }
        super.dismiss();
    }
}
